<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>AK JOB</title>
    <link rel="icon" type="image/x-icon" href="../../../layuiadmin/style/res/favicon.png"/>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="../../../layuiadmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="../../../layuiadmin/style/admin.css" media="all">
</head>
<body>

<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <table id="LAY-role-manage" lay-filter="LAY-role-manage"></table>
        </div>
    </div>
</div>

<script type="text/html" id="toolbarDemo">
    <button class="layui-btn layui-btn-sm" lay-event="execute">执行一次</button>
</script>

<script type="text/html" id="clientTmp">
    {{#  if(d.clientUrl != null){ }}
    <span>{{d.clientUrl}}:{{d.clientPort}}</span>
    {{#  } }}
</script>

<script type="text/html" id="statusTmp">
    {{#  if(d.status === -1){ }}
    <a class="layui-btn layui-btn-danger layui-btn-xs">执行超时</a>
    {{#  } }}
    {{#  if(d.status === 0){ }}
    <a class="layui-btn layui-bg-orange layui-btn-xs">执行失败</a>
    {{#  } }}
    {{#  if(d.status === 1){ }}
    <a class="layui-btn layui-btn-xs">执行成功</a>
    {{#  } }}
</script>

<script type="text/html" id="table-tool">

</script>


<script src="../../../layuiadmin/layui/layui.js"></script>
<script src="../../../layuiadmin/lib/juicer-min.js"></script>

<script>
    layui.config({
        base: '../../../layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'applicationJob','table', 'setter', 'auth', 'AppHelper', 'routerHelper', 'tableWraper'], function () {
        var $ = layui.$
            , form = layui.form
            , setter = layui.setter
            , applicationJob = layui.applicationJob
            , table = layui.table
            , routerHelper = layui.routerHelper
            , tableWraper = layui.tableWraper
            , AppHelper = layui.AppHelper
            , util = layui.util
            , LayerPage = AppHelper.getLayerPage()
        ;
        var router = layui.router();
        var jobId = router.search.jobId;

        table.on('toolbar(LAY-role-manage)', function(obj){
            switch(obj.event){
                case 'execute':
                    layer.prompt({title: '请输JSON格式自定义参数', formType: 2,
                        btn: ['执行', '关闭'],}, function(value, index, elem){
                        if(!isValidJson(value)){
                            layer.tips('不是有效的JSON格式', elem);
                            return ;
                        }
                        var loadIndex = layer.load(2);
                        applicationJob.methods.execute({id:jobId,paramsStr:value}).always(function () {
                            layer.close(loadIndex);
                        }).done(function (res) {
                            if (res.data) {
                                layer.msg("执行成功!", {icon: 1});
                                currentTable.reload();
                            }
                            layer.close(index);
                        });
                    });
                    break;
            };
        });

        //初始化表格
        var currentTable = tableWraper.render({
            elem: "#LAY-role-manage",
            toolbar: '#toolbarDemo',
            url: setter.apiHost + "/admin/api/akJobTask/pageList/" + jobId,
            cols: [[
                {title: "调度时间", field: 'startTime'
                    ,search: {title: "开始时间", type: "datetime",field : "startTime"}
                }
                , {title: "结束时间", field: 'endTime'
                    ,search: {title: "结束时间", type: "datetime",field : "endTime"}
                }
                , {title: "执行结果", field: 'status', templet: "#statusTmp"
                    ,search: {title: "执行结果", type: "select",field : "status", items: [{text: "执行超时", value: "-1"},{text: "执行失败", value: "0"},{text: "执行成功", value: "1"}]}
                }
                , {title: "目标地址", field: 'clientPort', templet: "#clientTmp"}
                , {title: "结果日志", field: 'errorLog'}
                , {title: "执行参数", field: 'paramsStr'}
                , {title: "备注", field: 'remark', search: true}
            ]]
        });

        function isValidJson(value){
            if (!value && value.length == 0){
                return true;
            }
            try {
                let parsed  = JSON.parse(value);
                if (typeof parsed === 'object' && parsed !== null && !Array.isArray(parsed)) {
                    return true;
                }
            } catch (e) {
                return false;
            }
            return false;
        }
    });
</script>
</body>
</html>
